package com.jltx.xq.coroutinesapp

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import retrofit2.await

/**
 * @author aidenquan
 *         Created by junlintianxia on 2021/07/11.
 */
class MainViewModel : ViewModel() {
    val statusLiveData = MutableLiveData<Boolean>()
    val picListLiveData = MutableLiveData<OpenApiResponse<List<OpenApiPicture>>>()

    fun getPictureCallWithCoroutine() {
        viewModelScope.launch {
            statusLiveData.value = true
            try {
                val pictureList = RetrofitService.service.getPicturesAsync(1, 15).await()
                statusLiveData.value = false
                picListLiveData.value = pictureList
            } catch (e: Exception) {
                statusLiveData.value = false
                picListLiveData.value = OpenApiResponse(-100, "获取图片数据失败", emptyList())
            }
        }
    }
}